package cn.com.cym.gamezao.web.recruit;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.BaseController;
import cn.com.cym.gamezao.base.BaseService;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.dto.UserJobDto;
import cn.com.cym.gamezao.entity.recruit.Job;
import cn.com.cym.gamezao.entity.xmu.Schedule;
import cn.com.cym.gamezao.service.recruit.JobService;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "r-岗位相关")
@RestController
@RequestMapping(value = "job")
@AuthToken(rule = "recruit.job")
public class JobController extends BaseController<Job> {

    final JobService thisService;

    public JobController(JobService jobService){
        this.thisService=jobService;
    }

    @Override
    public BaseService<Job> getService() {
        return this.thisService;
    }

    @Override
    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "按id获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{id}")
    @AuthToken(rule = "search",require = false)
    public ResponseEntity<?> getById(@PathVariable Long id) {
        return super.getById(id);
    }

    @Override
    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken(rule = "search",require = false)
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage) {
        return super.page(searchPage);
    }

    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "申请岗位！")
    @ApiOperationSupport(order = 5, author = "mr.chen")
    @PutMapping("/apply/{id}")
    @AuthToken(rule = "apply")
    public ResponseEntity<?> apply(@PathVariable @NonNull Long id){
        this.thisService.apply(id);
        return ResponseEntity.ok(new ResultResponse("提交申请成功，等待审核", HttpStatus.OK.value()));
    }

    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "审核岗位！")
    @ApiOperationSupport(order = 6, author = "mr.chen")
    @PostMapping("/audit")
    @AuthToken(rule = "audit")
    public ResponseEntity<?> audit(@RequestBody @NonNull UserJobDto userJob){
        //UserJob userJob1=this.userJobService.audit(userJob);
        return ResponseEntity.ok(null);
    }

    @ApiImplicitParam(name = "job", value = "job", required = true,dataType = "Job")
    @ApiOperation(value = "新建岗位")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @PostMapping()
    public ResponseEntity<?> add(@RequestBody Job job){
        final Job storeJob = this.thisService.save(job);
        return ResponseEntity.ok(storeJob);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id",paramType = "path", required = true,dataType = "Long"),
            @ApiImplicitParam(name = "job", value = "job",paramType = "", required = true,dataType = "Job")})
    @ApiOperation(value = "更新岗位")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@RequestBody Job job,@PathVariable Long id){
        final Job storeJob = this.thisService.update(job,id);
        return ResponseEntity.ok(storeJob);
    }

    @ApiImplicitParam(name = "file", value = "file", required = true,dataType = "MultipartFile",allowMultiple=true)
    @ApiOperation(value = "导入值班记录")
    @ApiOperationSupport(order = 6, author = "mr.chen")
    @PostMapping(value="import",produces={MediaType.ALL_VALUE})
    @AuthToken(rule = "import",require = false)
    public ResponseEntity<?> read(@RequestParam("file") MultipartFile file) {
        try {
            final ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> jobs = reader.read(2, 3, 80);
            for(Map<String, Object> maps:jobs){
                Job job = new Job();
                job.setJobUnit(String.valueOf(maps.get("招聘单位"))).setMainDepartment(String.valueOf(maps.get("主管部门")))
                                .setJobType(String.valueOf(maps.get("岗位类别"))).setCode(String.valueOf(maps.get("岗位编码")))
                                .setName(String.valueOf(maps.get("岗位名称"))).setNums(Integer.valueOf(String.valueOf(maps.get("招聘人数"))))
                                .setMaxAge(Integer.valueOf(String.valueOf(maps.get("最高年龄")))).setSexLimit(String.valueOf(maps.get("性别")))
                                .setLowBachelor(String.valueOf(maps.get("最低学历"))).setLowDegree(String.valueOf(maps.get("最低学位")))
                                .setProfession(String.valueOf(maps.get("所学专业"))).setOtherCondition(String.valueOf(maps.get("其他条件")));
                // System.out.println(maps);
                thisService.save(job);
            }
        }catch (IOException e){
            e.getMessage();
        }
        return ResponseEntity.ok("导入成功");
    }
}
